// Package logic
// @Author zhongxc
// @Date 2023/12/15 11:52:00
// @Desc
package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"time"
	"zhongxc.cc/admin-user-api/internal/svc"
	"zhongxc.cc/admin-user-api/internal/types"
	"zhongxc.cc/grpc-common/admin-user/client/login"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.LoginResponse, err error) {
	// todo: add your logic here and delete this line
	ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancelFunc()

	loginRes, err := l.svcCtx.LoginRpc.Login(ctx, &login.LoginReq{
		Username: req.Username,
		Password: req.Password,
	})
	if err != nil {
		logx.Error(err)
		return nil, err
	}
	return &types.LoginResponse{
		User:         loginRes.User,
		AccessToken:  loginRes.AccessToken,
		RefreshToken: loginRes.RefreshToken,
	}, nil
}
